package com.example.springbootweb.config;

import com.example.springbootweb.component.LoginHandlerInterceptor;
import com.example.springbootweb.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Locale;


@Configuration
public class MyMvcConfig implements WebMvcConfigurer {




    /**
     * 添加静态资源文件，外部可以直接访问地址,静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    //将组件注册进容器
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            //注册视图转发器
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/main.html").setViewName("dashboard");
                registry.addViewController("/abc").setViewName("success");
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
            }
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //静态资源
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/","/index.html","/user/login","/asserts/**","/webjars/**","/favicon.ico");
            }


        };

        return adapter;
    }
    /**
     * 添加自己的区域信息解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new  MyLocaleResolver();
    }
}
